package com.imd.s9.impl;

import com.imd.base.BusinessException;
import com.imd.s9.entity.TableInfo;
import com.imd.s9.service.SaleService;
import com.imd.s9.util.InitCriteria;
import com.imd.utils.SequenceUtil;
import com.imd.vo.s3.UserVO;
import org.bson.Document;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SaleImpl implements SaleService {

    @Resource
    MongoTemplate mongoTemplate;

    @Resource
    InitCriteria initCriteria;

    @Override
    public void autoCreateOrder() throws BusinessException {
        String tenantId = "66b48620495f4c20811b5b7d";
        List<String> types = new ArrayList<>();
        types.add("生产辅料");
        types.add("原材料");
        Query query = initCriteria.create(tenantId,Criteria.where("最低预警").gt("数量"),Criteria.where("分类").in(types));
        List<Document> list = mongoTemplate.find(query, Document.class, TableInfo.materialMana);
        if (list.isEmpty()){return;}
        Query query1 = initCriteria.create(tenantId,Criteria.where("备注").is("最低预警备货"),Criteria.where("单据状态").is(false));
        //Document temp = mongoTemplate.findOne(query1,Document.class,TableInfo.requisitionO);
        List<Document> details = new ArrayList<>();
        for(Document doc:list){
            BigDecimal bg = new BigDecimal(ObjectUtils.isEmpty(doc.get("最低预警"))?0:doc.getDouble("最低预警")).
                    subtract(new BigDecimal( ObjectUtils.isEmpty(doc.get("数量"))?0:doc.getDouble("数量")));
            Document detail = new Document();
            detail.put("物料编码",doc.get("物料编码"));
            detail.put("物料名称",doc.get("物料名称"));
            detail.put("规格型号",doc.get("规格型号"));
            detail.put("单位",doc.get("单位"));
            detail.put("辅助数量",new Double(0));
            detail.put("数量",bg.doubleValue());
            detail.put("税率",doc.get("默认税率"));
            detail.put("备注","");
            detail.put("原单号","");
            detail.put("到货要求","");
            details.add(detail);
        }
        Update update = new Update();
        update.set("单据状态",false);
        update.set("单据编号", SequenceUtil.getSequence("QGDD"));
        update.set("请购时间",new Date());
        update.set("经手人","system");
        update.set("明细",details);
        update.set("createName","system");
        update.set("createTime",new Date());
        update.set("isDelete",0);
        update.set("tenantId",TableInfo.tenantId);
        mongoTemplate.upsert(query1,update,TableInfo.requisitionO);
    }

    @Override
    public void autoNotify() throws BusinessException {
        String tenantId = "66b48620495f4c20811b5b7d";
        LocalDate nowTime = new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        Query query = initCriteria.create(tenantId,Criteria.where("单据状态").is(true),Criteria.where("实际来料时间").isNull());
        List<Document> list =  mongoTemplate.find(query,Document.class,TableInfo.saleO);
        if (list.isEmpty()){
            return;
        }
        for(Document doc:list){
            if(ObjectUtils.isEmpty(doc.get("订单日期"))||ObjectUtils.isEmpty(doc.getDate("预计来料时间"))){
                continue;
            }
           LocalDate ld1 = doc.getDate("订单日期").toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
           LocalDate ld2 = doc.getDate("预计来料时间").toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
           long nowDays = ChronoUnit.DAYS.between(nowTime,ld2);
           long days = ChronoUnit.DAYS.between(ld1, ld2);
           if(new BigDecimal(days).multiply(new BigDecimal(0.5)).equals(new BigDecimal(nowDays))){
               notifyMsg(doc);
           }
           if(new BigDecimal(days).multiply(new BigDecimal(0.25)).equals(new BigDecimal(nowDays))){
               notifyMsg(doc);
           }
        }

    }

    void notifyMsg(Document doc) throws BusinessException {
        String tenantId = doc.getString("tenantId");
        Query query = initCriteria.create(tenantId,Criteria.where("所属部门").is("采购部"));
        List<Document> users = mongoTemplate.find(query,Document.class,TableInfo.user);
        if(!users.isEmpty()){
            for(Document user:users){
                String userId = user.getString("id");

            }
        }
    }

    public static void main(String[] args) {
        Thread t = new Thread(() -> {

        });
    }
}
